package com.own.business.system.dictionary.business.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.own.business.system.dictionary.adapter.base.BaseDictionaryModel;
import com.own.business.system.dictionary.entity.map.SystemDictionaryItemMapVo;
import com.own.business.system.dictionary.entity.po.SystemDictionary;
import com.own.business.system.dictionary.entity.po.SystemDictionaryItem;
import io.swagger.v3.oas.annotations.media.Schema;

import java.util.List;
import java.util.stream.Collectors;

/**
 * DictionaryModel
 *
 * @author chenxueli
 * @date 2023-06-21 10:35:00
 */
public class DictionaryModel implements BaseDictionaryModel {

    @Schema(title = "主键id")
    private final Long id;

    @Schema(title = "字典id")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private final Long parentId;

    @Schema(title = "项目名称")
    private final String name;

    @Schema(title = "数据项值")
    private final String value;

    @Schema(title = "图标")
    private final String icon;

    @Schema(title = "唯一编号")
    private final String code;

    @Schema(title = "顺序，默认数字越小约在前面")
    private final Integer sortOrder;

    /**
     * 子字典
     */
    private List<DictionaryModel> children;

    public DictionaryModel(SystemDictionaryItem item) {
        this.id = item.getId();
        this.parentId = item.getFkDictionaryId();
        this.name = item.getName();
        this.value = item.getValue();
        this.icon = item.getIcon();
        this.sortOrder = item.getSortOrder();
        this.code = null;
        this.children = null;
    }

    public DictionaryModel(SystemDictionary item) {
        this.id = item.getId();
        this.parentId = null;
        this.name = item.getName();
        this.value = null;
        this.icon = item.getIcon();
        this.sortOrder = item.getSortOrder();
        this.code = item.getCode();
        this.children = null;
    }

    public DictionaryModel(SystemDictionaryItemMapVo item) {
        this.id = item.getId();
        this.parentId = item.getDictionaryId();
        this.name = item.getName();
        this.value = item.getValue();
        this.icon = item.getIcon();
        this.sortOrder = null;
        this.code = null;
        this.children = null;
    }

    public DictionaryModel(SystemDictionaryItem item, List<SystemDictionaryItem> children) {
        this(item);
        this.children = children.stream().map(DictionaryModel::new).collect(Collectors.toList());
    }

    public DictionaryModel(SystemDictionary item, List<SystemDictionaryItem> children) {
        this(item);
        this.children = children.stream().map(DictionaryModel::new).collect(Collectors.toList());
    }

    /**
     * 父字典id
     *
     * @return 父字典id
     */
    @Override
    public Long getParentId() {
        return this.parentId;
    }

    /**
     * 数据项值
     *
     * @return 数据项值
     */
    @Override
    public String getValue() {
        return this.value;
    }

    /**
     * 主键id
     *
     * @return 主键id
     */
    @Override
    public Long getId() {
        return this.id;
    }

    /**
     * 字典名称
     *
     * @return 字典名称
     */
    @Override
    public String getItemName() {
        return this.name;
    }

    /**
     * 唯一编号
     *
     * @return 唯一编号
     */
    @Override
    public String getCode() {
        return this.code;
    }

    /**
     * 图标
     *
     * @return 图标
     */
    @Override
    public String getIcon() {
        return this.icon;
    }

    /**
     * 顺序，默认数字越小约在前面
     *
     * @return 顺序，默认数字越小约在前面
     */
    @Override
    public Integer getSortOrder() {
        return this.sortOrder;
    }

    /**
     * 子字典
     *
     * @return 子字典
     */
    @Override
    public List<? extends BaseDictionaryModel> getChildren() {
        return this.children;
    }

}
